package ttOrder.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ttOrder.dao.DinnertableMapper;
import ttOrder.dao.FoodMapper;
import ttOrder.dao.MycartMapper;
import ttOrder.dao.OrderMapper;
import ttOrder.model.Dinnertable;
import ttOrder.model.Food;
import ttOrder.model.Order;
import ttOrder.model.OrderDetall;
import ttOrder.vo.DinnertableVO;
import ttOrder.vo.FoodVO;
import ttOrder.vo.OrderDetallVO;
import ttOrder.vo.OrderVO;
@Service
@Transactional
public class OrderService implements IOrderService {

	@Autowired
	public OrderMapper orderMapper;
	@Autowired
	public FoodMapper foodMapper;
	@Autowired
	public DinnertableMapper tableMapper;
	@Autowired
	public MycartMapper mycarMapper;
	
	//显示全部订单
	@Override
	public List<OrderVO> findAllOrder() {
		List<Order> list=orderMapper.selectAll();
		if(list != null){
			OrderVO vo=null;
			List<OrderVO> resultList = new ArrayList<OrderVO>();
			for(Order order : list){
				vo=new OrderVO();
				BeanUtils.copyProperties(order, vo);
				resultList.add(vo);
			}
			return resultList;
		}
		return null;
	}

	//根据订单号查询订单详情
	@Override
	public List<OrderDetallVO> findDetallById(Integer orderId) {
		
		List<OrderDetall> list=orderMapper.findDetallById(orderId);
		if(list!=null){
			OrderDetallVO vo=null;
			List<OrderDetallVO> resultList=new ArrayList<OrderDetallVO>();
			for(OrderDetall orderDetall:list){
				vo=new OrderDetallVO();
				BeanUtils.copyProperties(orderDetall,vo);
				
				Food food=foodMapper.selectFoodById(orderDetall.getFoodId());
				if(food!=null){
					vo.setFoodName(food.getFoodName());
					vo.setPrice(food.getPrice());
				}	
				resultList.add(vo);
				//System.out.println(vo.getFoodName());
			}
			return resultList;
		}	
		return null;
	}

	@Override
	public void insertOrder(OrderVO orderVO) {

	}

	//修改订单状态
	@Override
	public void updateOrder(OrderVO orderVO) {
		Order order=new Order();
		BeanUtils.copyProperties(orderVO,order);
		orderMapper.edit(order);

	}
	
	//删除订单
	@Override
	public void deleteOrder(Integer id) {
		orderMapper.deleteById(id);
	}

	//根据订单号查询订单
	@Override
	public OrderVO findOrderById(Integer orderId) {
		Order entity=orderMapper.findOrderById(orderId);
		if(entity!=null){
			OrderVO vo=new OrderVO();
			BeanUtils.copyProperties(entity, vo);
			
			Dinnertable table=tableMapper.selectById(entity.getTableId());
			if(table!=null){
				vo.setTableName(table.getTableName());
			}
	
			return vo;
		}
		return null;
	}

	@Override
	public void deleteDetall(Integer id) {
		orderMapper.deleteDetall(id);
		
	}

	//用户选择餐桌后新增订单
	@Override
	public void addOrder(Integer tableId) {
		orderMapper.addOrder(tableId);
	}

	

}
